package utilities;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import entities.Genre;
import entities.Movie;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Hacnar
 */
public class MovieConverter implements Converter {

    @Override
    public void marshal(Object o, HierarchicalStreamWriter writer, MarshallingContext mc) {
        Movie movie = (Movie) o;
        writer.startNode("title");
        writer.setValue(movie.getTitle());
        writer.endNode();
        writer.startNode("runtime");
        writer.setValue(""+movie.getRunTime());
        writer.endNode();
        writer.startNode("cast");
        if(movie.getCast().size()!=0)
        {
            for (String s : movie.getCast()) {
                writer.startNode("actor");
                writer.startNode("name");
                writer.setValue(s);
                writer.endNode();
                writer.endNode();
            }
        }
        writer.endNode();
        writer.startNode("directors");
        if(movie.getDirectors().size()!=0)
        {
            for (String s : movie.getDirectors()) {
                writer.startNode("director");
                writer.startNode("name");
                writer.setValue(s);
                writer.endNode();
                writer.endNode();
            }
        }
        writer.endNode();
        writer.startNode("releaseYear");
        writer.setValue(""+movie.getReleaseYear());
        writer.endNode();
        writer.startNode("genres");
        for (Genre g : movie.getGenres()) {
            writer.startNode("genre");
            writer.setValue(toUpperCamel(g.toString()));
            writer.endNode();
        }
        writer.endNode();
        writer.startNode("countriesOfOrigin");
        for (String s : movie.getCountriesOfOrigin()) {
            writer.startNode("country");
            writer.setValue(s);
            writer.endNode();
        }
        writer.endNode();
        if (movie.getNote() != null && !"".equals(movie.getNote())) {
            writer.startNode("note");
            writer.setValue(movie.getNote());
            writer.endNode();
        }
        
    }

    @Override
    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext uc) {
        Movie movie = new Movie();
        reader.moveDown();
        movie.setTitle(reader.getValue());
        reader.moveUp();
        reader.moveDown();
        movie.setRunTime(Integer.parseInt(reader.getValue()));
        reader.moveUp();
        reader.moveDown();
        List<String> cast = new ArrayList<String>();
        while(reader.hasMoreChildren()) {
            reader.moveDown();
            reader.moveDown();
            cast.add(reader.getValue());
            reader.moveUp();
            reader.moveUp();
        }
        movie.setCast(cast);
        reader.moveUp();
        reader.moveDown();
        List<String> directors = new ArrayList<String>();
        while(reader.hasMoreChildren()) {
            reader.moveDown();
            reader.moveDown();
            directors.add(reader.getValue());
            reader.moveUp();
            reader.moveUp();
        }
        movie.setDirectors(directors);
        reader.moveUp();
        reader.moveDown();
        movie.setReleaseYear(Integer.parseInt(reader.getValue()));
        reader.moveUp();
        reader.moveDown();
        List<Genre> genres = new ArrayList<Genre>();
        while(reader.hasMoreChildren()) {
            reader.moveDown();
            genres.add(Enum.valueOf(Genre.class, reader.getValue().toUpperCase()));
            reader.moveUp();
        }
        movie.setGenres(genres);
        reader.moveUp();
        reader.moveDown();
        List<String> countries = new ArrayList<String>();
        while(reader.hasMoreChildren()) {
            reader.moveDown();
            countries.add(reader.getValue());
            reader.moveUp();
        }
        movie.setCountriesOfOrigin(countries);
        reader.moveUp();
        if (reader.hasMoreChildren()) {
            reader.moveDown();
            movie.setNote(reader.getValue());
            reader.moveUp();
        }
        return movie;
    }

    @Override
    public boolean canConvert(Class type) {
        return type.equals(Movie.class);
    }
    
    
    private String toUpperCamel(String string) {
        return string.substring(0,1).toUpperCase()+string.substring(1,string.length()).toLowerCase();
    }
}
